package com.itheima.mall.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.exception.ErrorResult;
import com.itheima.mall.mapper.HomeRecommendProductMapper;
import com.itheima.mall.pojo.HomeNewProduct;
import com.itheima.mall.pojo.HomeRecommendProduct;
import com.itheima.mall.service.SmsHomeRecommendProductService;
import com.itheima.mall.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SmsHomeRecommendProductServiceImpl extends ServiceImpl<HomeRecommendProductMapper, HomeRecommendProduct> implements SmsHomeRecommendProductService {

    @Autowired
    private HomeRecommendProductMapper homeRecommendProductMapper;

    /**
     * 添加首页推荐
     */
    @Override
    public void createRecommend(List<HomeRecommendProduct> homeRecommendProductList) {
        if (CollUtil.isEmpty(homeRecommendProductList)){
            throw new BusinessException(ErrorResult.error());
        }
        homeRecommendProductList = homeRecommendProductList.stream().map(itme -> {
            itme.setSort(100);
            return itme;
        }).collect(Collectors.toList());
        this.saveBatch(homeRecommendProductList);
    }

    /**
     * 批量删除推荐
     */
    @Override
    public void deleteRecommend(List<Long> ids) {
        if (CollUtil.isEmpty(ids)){
            throw new BusinessException(ErrorResult.error());
        }
        this.removeBatchByIds(ids);
    }

    /**
     * 分页查询推荐
     */
    @Override
    public PageResult listRecommend(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        Page<HomeRecommendProduct> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<HomeRecommendProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(productName != null, HomeRecommendProduct::getProductName,productName)
                .eq(recommendStatus != null,HomeRecommendProduct::getRecommendStatus,recommendStatus)
                .orderByAsc(HomeRecommendProduct::getSort);
        Page<HomeRecommendProduct> pageInfo = homeRecommendProductMapper.selectPage(page, queryWrapper);
        return new PageResult(pageNum,pageSize, (int) pageInfo.getTotal(),pageInfo.getRecords());
    }

    /**
     * 批量修改推荐状态
     */
    @Override
    public void updateStatus(List<Long> ids, Integer recommendStatus) {
        LambdaUpdateWrapper<HomeRecommendProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.in(CollUtil.isNotEmpty(ids), HomeRecommendProduct::getId,ids)
                .set(recommendStatus != null,HomeRecommendProduct::getRecommendStatus,recommendStatus);
        homeRecommendProductMapper.update(null,updateWrapper);
    }

    /**
     * 修改推荐排序
     */
    @Override
    public void updateSort(Long id, Integer sort) {
        LambdaUpdateWrapper<HomeRecommendProduct> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(HomeRecommendProduct::getSort,sort)
                .eq(HomeRecommendProduct::getId,id);
        homeRecommendProductMapper.update(null,updateWrapper);
    }


}
